package com.zhang.learn.javaassist.controller;

import com.zhang.learn.javaassist.dto.FileInfoDto;
import com.zhang.learn.javaassist.service.ExcelProcesser;
import com.zhang.learn.javaassist.service.WordProcesser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhanghaolian
 * @date 2021/7/21 21:36
 */
@RestController
@Slf4j
public class EasyPoiTestController {
    @Autowired
    private ExcelProcesser excelProcesser;
    @Autowired
    private WordProcesser wordProcesser;

    @RequestMapping("/test")
    public void test(String dirPath, HttpServletRequest request, HttpServletResponse response) {
        // 获取所有的word excel文件列表
        List<FileInfoDto> list = process(Paths.get(dirPath.trim()).toFile());
        // 导出文件列表,本地导出
        //excelProcesser.outputResult(list);
        // httpResponse 导出
        excelProcesser.outputResult(list,FileInfoDto.class,request,response);
    }

    public List<FileInfoDto> process(File file) {
        List<FileInfoDto> list = new ArrayList<>();
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File tempFile: files) {
                    list.addAll(process(tempFile));
                }
            }else {
                String fileName = file.getName();
                String sysPath = file.getAbsolutePath();
                String suffix = getSuffix(sysPath);
                if (StringUtils.isBlank(suffix)) {
                    return list;
                }
                FileInfoDto fileInfoDto = new FileInfoDto();
                fileInfoDto.setFileName(fileName);
                fileInfoDto.setSuffix(suffix);
                fileInfoDto.setSysPath(sysPath);
                if (suffix.equals("doc") || suffix.equals("docx")) {
                    wordProcesser.getPageCount(fileInfoDto);
                }else {
                    excelProcesser.getPageCount(fileInfoDto);
                }
                log.info(fileInfoDto.toString());
                list.add(fileInfoDto);
                return list;
            }
        }else {
            return list;
        }
        return list;
    }

    /**
     * 根据一个文件系统的绝对路径，来获取文件的后缀名
     * @param path
     * @return
     */
    public String getSuffix(String path) {
        if (StringUtils.isBlank(path)) {
            return null;
        }

        String regexp = "\\.(doc|docx|xls|xlsx)$";
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher = pattern.matcher(path.trim());
        if (matcher.find()) {
            return matcher.group(1);
        }else {
            return null;
        }

    }

}
